package net.zhnb.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import net.zhnb.bean.Shower;
import net.zhnb.dao.ShowerDao;
import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

public class ShowerDaoImpl extends MiddleAbstractDao<Shower> implements ShowerDao {

	@Override
	protected Class<Shower> getEntityClass() {
		return Shower.class;
	}

	@Override
	public PageList getPageList(Page page) throws Exception {
		StringBuffer whereSql = new StringBuffer();
		whereSql.append(" where 1 = 1");
		if(page.getParameter("roomid")!=null&&page.getParameter("roomid").equals("")==false){
			whereSql.append(" and a.roomid = " + page.getParameter("roomid"));
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from shower a inner join room b on a.roomid = b.id");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select a.*,b.roomname from shower a");
		queryListSql.append(" inner join room b on a.roomid = b.id");
		queryListSql.append( whereSql );
		queryListSql.append(" order by a.id desc");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<Map<String, Object>> dataList = super.findBySqlToMapList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}

	@Override
	public boolean deleteByRoomId(BigDecimal roomId) throws Exception {
		String sqlString = "delete from shower where roomid = " +roomId;
		return super.executeSql(sqlString);
	}

	@Override
	public Shower findShower(String tagId) throws Exception {
		String sqlString = "select b.* from tag a  INNER join shower b on a.showerid = b.id where a.id = "+tagId;
		return super.findBySqlToBean(sqlString);
	}
	

}
